<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\middleware\Auth;
use app\model\DistrictCodesModel;
use think\App;
use think\Exception;

class DiscountCode extends Base {

    protected $middleware = [Auth::class];

    private $req;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->discountCodeModel = new DistrictCodesModel();

        $this->uid = $this->isLogin();
    }


    /*
     * 创建订单
     * */
    public function getDiscountCodeInfo(){

        if(!isset($this->req["district_code"]) || empty($this->req["district_code"])){
            return $this->fail("优惠码格式不正确");
        }

        $districtCode = trim($this->req["district_code"]);
        if(strlen($districtCode) != 8){
            return $this->fail("优惠码不存在");
        }

        try {
            $districtCodeInfo = $this->discountCodeModel->useDistrictCode($this->uid,$districtCode);
        }catch (Exception $e){
            return $this->fail($e->getMessage());
        }

        return  $this->succ($districtCodeInfo);

    }




}
